package iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MenuItem;
import android.view.VelocityTracker;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.R;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.entity.Product;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.fragment.producerFragment.FragmentAddProduct;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.fragment.producerFragment.FragmentProducerProfile;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.fragment.producerFragment.FragmentProductManagement;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.fragment.producerFragment.FragmentProfile;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.service.IProductService;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.service.serviceConnection.LoginServiceConnection;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.service.serviceConnection.ProductServiceConnection;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.service.serviceImpl.LoginService;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.service.serviceImpl.ProductService;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.utils.JSONObjectUtils;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.utils.QRCodeUtils;

public class ActivityProducer extends AppCompatActivity {

    private BottomNavigationView bottomNavigationView;
    private GestureDetector gestureDetector;
    private VelocityTracker velocityTracker = null;
    private FragmentProductManagement fragmentProductManagement;

    // 登录相关服务类
    private ProductServiceConnection productConnection;


    @Override
    protected void onStart() {
        super.onStart();

        productConnection = new ProductServiceConnection(service -> {
            List<Product> products = (List<Product>) getIntent().getSerializableExtra("productsImported");
            if (products != null) {
                boolean resultImport = service.importProductsFromOtherFiles(products);
                if (!resultImport) {
                    Toast.makeText(this, "添加失败", Toast.LENGTH_SHORT).show();
                }
            }

            fragmentProductManagement.onServiceConnected(service);
        });
        Intent intent = new Intent(this, ProductService.class);
        bindService(intent, productConnection, BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (productConnection.isBound()) {
            unbindService(productConnection);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_producer);

        bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(navListener);
        fragmentProductManagement = new FragmentProductManagement();

        // 默认选择第一个Fragment
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container,fragmentProductManagement)
                    .commitNow();
        }
    }


    // 底部导航的监听事件
    private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment selectedFragment = null;

            int itemId = item.getItemId();
            if (itemId == R.id.nav_hot) {
                selectedFragment = new FragmentProductManagement();
            } else if (itemId == R.id.nav_library) {
                selectedFragment = new FragmentAddProduct(productConnection);
            } else if (itemId == R.id.nav_profile) {
                selectedFragment = new FragmentProducerProfile();
            }

            switchToFragment(selectedFragment);

            return true;
        }
    };


    // 跳转Fragment
    private void switchToFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, fragment)
                .commitNow();
        if (fragment instanceof FragmentProductManagement) {
            ((FragmentProductManagement) fragment).onServiceConnected(productConnection.getService());
        }
    }
}